#include "RadiusOutlierModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.radius_outlier_model.json";
}

RadiusOutlierModel::RadiusOutlierModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/remove_outliers.html");
}

RadiusOutlierModel& RadiusOutlierModel::get()
{
    static RadiusOutlierModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void RadiusOutlierModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    sampleSize_ = json["sampleSize"].toInt(sampleSize_);
    sampleMinPos_ = json["sampleMinPos"].toDouble(sampleMinPos_);
    sampleMaxPos_ = json["sampleMaxPos"].toDouble(sampleMaxPos_);

    removalType_ = json["removalType"].toInt(removalType_);
    radiusSearch_ = json["radiusSearch"].toDouble(radiusSearch_);
    minNeighborsInRadius_ = json["minNeighborsInRadius"].toInt(minNeighborsInRadius_);
    keepOrganized_ = json["keepOrganized"].toBool(keepOrganized_);
    conditionFieldName_ = json["conditionFieldName"].toString(conditionFieldName_);
    conditionMin_ = json["conditionMin"].toDouble(conditionMin_);
    conditionMax_ = json["conditionMax"].toDouble(conditionMax_);
}

QJsonObject RadiusOutlierModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("sampleSize", QJsonValue(sampleSize_));
    json.insert("sampleMinPos", QJsonValue(sampleMinPos_));
    json.insert("sampleMaxPos", QJsonValue(sampleMaxPos_));

    json.insert("removalType", QJsonValue(removalType_));
    json.insert("radiusSearch", QJsonValue(radiusSearch_));
    json.insert("minNeighborsInRadius", QJsonValue(minNeighborsInRadius_));
    json.insert("keepOrganized", QJsonValue(keepOrganized_));
    json.insert("conditionFieldName", QJsonValue(conditionFieldName_));
    json.insert("conditionMin", QJsonValue(conditionMin_));
    json.insert("conditionMax", QJsonValue(conditionMax_));

    return json;
}
